package com.gx.smart.base;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.blankj.utilcode.util.BarUtils;
import com.gx.smart.base.databinding.FragmentRoot2Binding;
import com.gx.smart.base.loading.LoadingView;

import org.greenrobot.eventbus.EventBus;


/**
 * 基础fragment
 */

public abstract class BaseMVVMFragmentV2<VM extends BaseViewModel> extends BaseFragment {

    protected VM viewModel;
    protected FragmentRoot2Binding binding;
    protected LoadingView mLoadingInitView;
    protected View mNoDataView;
    protected TextView mTxtTitle;
    protected TextView mLeft;
    protected TextView mRight;
    protected ImageView mRightImage;
    protected RelativeLayout mViewStubContent;
    protected View viewTooBar;
    private ViewStub mViewStubToolbar;
    private ViewStub mViewStubInitLoading;
    private ViewStub mViewStubNoData;
    private View rootView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getActivity().getApplication())).get(onBindViewModel());
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentRoot2Binding.inflate(inflater, container, false);
        rootView = binding.getRoot();
        initView(rootView);
        initContent();
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        initData();
        initObserver();
    }

    @Override
    protected void initView(@NonNull View view) {
        View statBar = view.findViewById(R.id.statusBar);
        ViewGroup.LayoutParams params = statBar.getLayoutParams();
        params.height = BarUtils.getStatusBarHeight();
        statBar.setLayoutParams(params);
    }

    /**
     * 初始化内容
     */
    protected void initContent() {
        initCommonView(binding.getRoot());
    }

    public void initCommonView(View view) {
        mViewStubToolbar = view.findViewById(R.id.view_stub_toolbar);
        mViewStubContent = view.findViewById(R.id.view_stub_content);
        mViewStubInitLoading = view.findViewById(R.id.view_stub_init_loading);
        mViewStubNoData = view.findViewById(R.id.view_stub_nodata);

        if (enableToolbar()) {
            mViewStubToolbar.setLayoutResource(onBindToolbarLayout());
            viewTooBar = mViewStubToolbar.inflate();
            initTooBar(viewTooBar);
        }
        initContentView(mViewStubContent);
    }

    public void initContentView(ViewGroup root) {
        LayoutInflater.from(getContext()).inflate(onBindLayout(), root, true);
    }

    public abstract int onBindLayout();

    public boolean enableToolbar() {
        return true;
    }


    protected void initTooBar(View view) {
        mLeft = view.findViewById(R.id.left);
        mTxtTitle = view.findViewById(R.id.center_title);
        mRight = view.findViewById(R.id.right);
        mRightImage = view.findViewById(R.id.rightImageView);
        if (getLeft()) {
            mLeft.setVisibility(View.VISIBLE);
            mLeft.setOnClickListener(v -> getActivity().onBackPressed());
        }

        if (!TextUtils.isEmpty(getToolbarTitle())) {
            mTxtTitle.setVisibility(View.VISIBLE);
            mTxtTitle.setText(getToolbarTitle());
        }

        if (!TextUtils.isEmpty(getRight())) {
            mRight.setVisibility(View.VISIBLE);
            mRight.setText(getRight());
        }

        if (getRightImage() != -1) {
            mRightImage.setVisibility(View.VISIBLE);
            mRightImage.setImageResource(getRightImage());
        }
    }


    public boolean getLeft() {
        return true;
    }

    public String getRight() {
        return "";
    }

    public int getRightImage() {
        return -1;
    }

    public abstract String getToolbarTitle();


    public int onBindToolbarLayout() {
        return R.layout.layout_common_title;
    }


    public void showInitLoadView(boolean show) {
        if (mLoadingInitView == null) {
            View view = mViewStubInitLoading.inflate();
            mLoadingInitView = view.findViewById(R.id.loadingView);
        }
        mLoadingInitView.setVisibility(show ? View.VISIBLE : View.GONE);
    }


    public void showNoDataView(boolean show) {
        if (mNoDataView == null) {
            View view = mViewStubNoData.inflate();
            mNoDataView = view.findViewById(R.id.view_no_data);
        }
        mNoDataView.setVisibility(show ? View.VISIBLE : View.GONE);
    }


    protected void registerEventBus() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    protected void unregisterEventBus() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    protected abstract Class<VM> onBindViewModel();


    /**
     * 网络请求数据
     */
    @Override
    protected void initData() {

    }

    /**
     * 获取viewModel中的数据
     */
    protected void initObserver() {
    }
}
